package com.winning.pmph.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winning.pmph.entity.TaskItem;
import com.winning.pmph.mapper.TaskItemMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wu_di@winning.com.cn
 * @date 2020/11/25
 */
@Component
@Transactional
public class TaskItemService extends ServiceImpl<TaskItemMapper, TaskItem> {
    public TaskItem getLastFinishedTaskItemByKnowledgeId(String knowledgeId) {
        List<TaskItem> taskItemList = lambdaQuery()
                .eq(TaskItem::getKnowledgeId, knowledgeId)
                .eq(TaskItem::getDeleted, TaskItem.NO_DELETED)
                .eq(TaskItem::getStatus, TaskItem.STATUS_RELEASED)
                .orderByDesc(TaskItem::getUpdatedTime)
                .orderByAsc(TaskItem::getId)
                .list();
        if (CollectionUtils.isNotEmpty(taskItemList)) {
            return taskItemList.get(0);
        }
        return null;
    }
    // 获取当前, 审核完成, 未发布的知识子任务;
    public TaskItem getPassedTaskItemByKnowledgeId(String knowledgeId) {
        List<TaskItem> taskItemList = lambdaQuery()
                .eq(TaskItem::getKnowledgeId, knowledgeId)
                .eq(TaskItem::getDeleted, TaskItem.NO_DELETED)
                .eq(TaskItem::getStatus, TaskItem.STATUS_PASS)
                .orderByDesc(TaskItem::getUpdatedTime)
                .orderByAsc(TaskItem::getId)
                .list();
        if (CollectionUtils.isNotEmpty(taskItemList)) {
            return taskItemList.get(0);
        }
        return null;
    }
    // 获取当前,未发布的知识子任务;
    public  List<TaskItem> getRevisionTaskItemByKnowledgeId(String knowledgeId) {
        List<TaskItem> taskItemList = lambdaQuery()
                .eq(TaskItem::getKnowledgeId, knowledgeId)
                .eq(TaskItem::getDeleted, TaskItem.NO_DELETED)
                .ne(TaskItem::getStatus, TaskItem.STATUS_RELEASED)
                .orderByDesc(TaskItem::getUpdatedTime)
                .orderByAsc(TaskItem::getId)
                .list();
        if (CollectionUtils.isNotEmpty(taskItemList)) {
            return  taskItemList;
        }
        return null;
    }
}
